package com.rabbiter.oes.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.config.EasyExcelConfig;
import com.rabbiter.oes.entity.ApiResult;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.entity.Student1;
import com.rabbiter.oes.service.Student1Service;
import com.rabbiter.oes.serviceimpl.StudentServiceImpl;
import com.rabbiter.oes.util.ApiResultHandler;
import com.rabbiter.oes.vo.StudentExcelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private Student1Service student1Service;
    private final ApplicationContext applicationContext = null;
    @Autowired
    private StudentServiceImpl studentService;
    @Resource
    private DataSource dataSource;
    @GetMapping("/students/{page}/{size}/{name}/{grade}/{tel}/{institute}/{major}/{clazz}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size,
                             @PathVariable  String name, @PathVariable String grade,
                             @PathVariable String tel, @PathVariable String institute,
                             @PathVariable String major, @PathVariable String clazz) {
        Page<Student> studentPage = new Page<>(page,size);
        IPage<Student> res = studentService.findAll(
                studentPage, name, grade, tel, institute, major, clazz
        );
        return  ApiResultHandler.buildApiResult(200,"分页查询所有学生",res);
    }

    @GetMapping("/student/{studentId}")
    public ApiResult findById(@PathVariable("studentId") Integer studentId) {
        Student res = studentService.findById(studentId);
        if (res != null) {
        return ApiResultHandler.buildApiResult(200,"请求成功",res);
        } else {
            return ApiResultHandler.buildApiResult(404,"查询的用户不存在",null);
        }
    }

    @DeleteMapping("/student/{studentId}")
    public ApiResult deleteById(@PathVariable("studentId") Integer studentId) {
        return ApiResultHandler.buildApiResult(200,"删除成功",studentService.deleteById(studentId));
    }

    @PutMapping("/studentPWD")
    public ApiResult updatePwd(@RequestBody Student student) {
        studentService.updatePwd(student);
        return ApiResultHandler.buildApiResult(200,"密码更新成功",null);
    }
    @PutMapping("/student")
    public ApiResult update(@RequestBody Student student) {
        int res = studentService.update(student);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"更新成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"更新失败",res);
    }

    /**
     *
     * 添加单个学生
     * @param student
     * @return
     */
    @PostMapping("/student")
    public ApiResult add(@RequestBody Student student) {
        int res = studentService.add(student);
        if (res == 1) {
            return ApiResultHandler.buildApiResult(200,"添加成功",null);
        }else {
            return ApiResultHandler.buildApiResult(400,"添加失败",null);
        }
    }

    /**
     * 从excel批量添加学生
     * @param students
     * @return
     */
    @PostMapping("/studentList")
    public ApiResult addList(@RequestBody List<Student> students) {
        int res = studentService.addList(students);
        if (res == students.size()) {

            return ApiResultHandler.buildApiResult(200,"添加成功",null);
        }else {
            return ApiResultHandler.buildApiResult(400,"添加失败",null);
        }
    }

    /**
     * 获取所有学生信息
     * @return
     */
    @PostMapping("/student/all")
    public ApiResult findAllStudent() {
        List<StudentExcelVO> students = studentService.findByGroup();
        return ApiResultHandler.buildApiResult(200,"获取所有学生信息",students);
    }

    @PostMapping("/student/file")
    //嵌套获取文件
    public ApiResult file(MultipartFile file){
        System.out.println(file);
        //文件转化为输入流
        InputStream inputStream =null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //easyexcel的read方法进行数据读取
        EasyExcelConfig readListener = new EasyExcelConfig(student1Service);
        EasyExcel.read(inputStream, Student1.class, readListener).sheet().doRead();
        if (readListener.getSuccessCount() > 0){
            return ApiResultHandler.buildApiResult(200,"添加成功",readListener.getSuccessCount());
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",readListener.getSuccessCount());
    }
}
